package com.seh.contentcenter.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seh.contentcenter.dto.AddressInfoDTO;
import com.seh.contentcenter.entity.AddressInfo;
import com.seh.contentcenter.mapper.AddressMapper;
import com.seh.contentcenter.service.AddressService;
import com.seh.contentcenter.utils.DTOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 新增地址信息
     *
     * @param addressInfo
     * @return
     */
    @Override
    public String addNewAddress(AddressInfo addressInfo) {
        int result = addressMapper.insert(addressInfo);
        if (result > 0) {
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 批量新增地址信息
     *
     * @param addressInfoList
     * @return
     */
//    @Override
    public String addNewAddresses(List<AddressInfoDTO> addressInfoList) {
        int successCount = 0;
        for (AddressInfoDTO addressInfoDTO : addressInfoList) {
            AddressInfo addressInfo = new AddressInfo();
            BeanUtil.copyProperties(addressInfoDTO, addressInfo);
            int result = addressMapper.insert(addressInfo);
            if (result > 0) {
                successCount++;
            }
        }
        if (successCount == addressInfoList.size()) {
            return "添加成功";
        } else if (successCount > 0) {
            return "部分地址添加成功";
        }
        return "添加失败";
    }

    /**
     * 通过用户id，查询用户保存的所有地址
     *
     * @param userId
     * @return
     */
    @Override
    public List<AddressInfoDTO> findAddressByUserId(Integer userId) {
        QueryWrapper<AddressInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<AddressInfo> addressInfos = addressMapper.selectList(queryWrapper);
        List<AddressInfoDTO> addressInfoDTOList = new ArrayList<>();
        addressInfos.forEach(f -> {
            AddressInfoDTO addressInfoDTO = DTOUtil.convertAddress(f);
            addressInfoDTOList.add(addressInfoDTO);
        });
        return addressInfoDTOList;
    }

    /**
     * 通过地址的主键id，获取地址的详细信息
     *
     * @param addressId
     * @return
     */
    @Override
    public AddressInfoDTO findAddressById(Integer addressId) {
        AddressInfo addressInfo = addressMapper.selectById(addressId);
        AddressInfoDTO addressInfoDTO = DTOUtil.convertAddress(addressInfo);
        return addressInfoDTO;
    }

    /**
     * 通过主键id，删除地址
     *
     * @param addressId
     * @return
     */
    @Override
    public String deleteAddressById(Integer addressId) {
        int i = addressMapper.deleteById(addressId);
        if (i == 1) {
            return "删除成功";
        }
        return "删除失败";
    }

    /**
     * 通过主键id，修改地址信息
     *
     * @param addressInfoDTO
     * @return
     */
    @Override
    public String updateAddressInfo(AddressInfoDTO addressInfoDTO) {
        AddressInfo addressInfo = new AddressInfo();
        BeanUtil.copyProperties(addressInfoDTO, addressInfo);
        int update = addressMapper.updateById(addressInfo);
        if (update == 1) {
            return "修改成功";
        }
        return "修改失败";
    }


}
